<template>
  <upload-excel :onSuccess="onSuccess"></upload-excel>
</template>

<script setup lang="ts">
import {} from 'vue'
import UploadExcel from '../../components/UploadExcel/index.vue'
import { USER_RELATIONS } from './utils'
import { userBatchImport } from '../../api/user'
import { useRouter } from 'vue-router'
const router = useRouter()
/**
 * 数据解析成功之后的回调
 */
const onSuccess = async (excelData: any) => {
  const { results } = excelData
  const updateData = generateData(results)
  await userBatchImport(updateData)
  router.push('/user/list')
}
/**
 * 筛选数据
 */
const generateData = (results: any) => {
  const arr = []
  results.forEach((item:any) => {
    const userInfo = {}
    Object.keys(item).forEach(key => {
      userInfo[USER_RELATIONS[key]] = item[key]
    })
    arr.push(userInfo)
  })
  return arr
}
</script>

<style lang="scss" scoped></style>
